package com.starlight.flower_server.domain.service.impl;

import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.loginInfo.data.LoginInfoData;
import com.starlight.flower_server.domain.service.LoginInfoService;
import com.starlight.flower_server.infrastructure.entity.FlowerUser;
import com.starlight.flower_server.infrastructure.entity.LoginInfo;
import com.starlight.flower_server.infrastructure.repository.FlowerUserRepository;
import com.starlight.flower_server.infrastructure.repository.FlowerUserRoleRepository;
import com.starlight.flower_server.infrastructure.repository.LoginInfoRepository;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/19 8:54
 */
@Service
public class LoginInfoServiceImpl implements LoginInfoService {

    @Resource
    private LoginInfoRepository loginInfoRepository;

    @Resource
    private FlowerUserRepository flowerUserRepository;

    @Override
    public LoginInfo queryLoginInfoDetailById(Long id) {
        return loginInfoRepository.queryDetail(id);
    }

    @Override
    public List<LoginInfoData> queryLoginInfoListByFlowerUserIdLatest10(Long flowerUserId) {
        List<LoginInfo> loginInfoList=loginInfoRepository.queryListByFlowerUserIdLatest10(flowerUserId);
        return loginInfoList
                .stream().map(loginInfo -> {
                    LoginInfoData loginInfoData=new LoginInfoData();
                    BeanUtils.copyProperties(loginInfo,loginInfoData);
                    return loginInfoData;
                }).toList();
    }

    @Override
    public List<LoginInfoData> queryAllLoginInfoListByFlowerUserId(Long flowerUserId) {
        List<LoginInfo> loginInfoList=loginInfoRepository.queryAllListByFlowerUserId(flowerUserId);
        return loginInfoList
                .stream().map(loginInfo -> {
                    LoginInfoData loginInfoData=new LoginInfoData();
                    BeanUtils.copyProperties(loginInfo,loginInfoData);
                    return loginInfoData;
                }).toList();
    }


    @Override
    public TotalData<LoginInfoData> queryAllLoginInfoData() {
        TotalData<LoginInfoData> data=new TotalData<>();
        List<LoginInfo> loginInfoList=loginInfoRepository.queryAll();
        List<LoginInfoData> loginInfoDataList=loginInfoList
                .stream().map(loginInfo -> {
                    LoginInfoData loginInfoData=new LoginInfoData();
                    BeanUtils.copyProperties(loginInfo,loginInfoData);
                    return loginInfoData;
                }).toList();
        Long total= (long) loginInfoDataList.size();
        data.setTable("LoginInfo");
        data.setStatTime(new Date());
        data.setTotal(total);
        data.setRecords(loginInfoDataList);
        return data;
    }

    @Override
    public TotalData<LoginInfoData> queryLoginInfoTotalCount() {
        TotalData<LoginInfoData> data=new TotalData<>();
        Long total= loginInfoRepository.queryTotalCount();
        data.setTable("LoginInfoData");
        data.setStatTime(new Date());
        data.setTotal(total);
        return data;
    }

    @Override
    public Integer insertLoginInfo(LoginInfo loginInfo) {
        return loginInfoRepository.insert(loginInfo);
    }
}
